package com.stm.web.controller.toolsTest;

import java.io.IOException;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.stm.common.annotation.Log;
import com.stm.common.core.controller.BaseController;
import com.stm.common.core.domain.AjaxResult;
import com.stm.common.core.page.TableDataInfo;
import com.stm.common.enums.BusinessType;
import com.stm.framework.util.ShiroUtils;
import com.stm.toolsTest.domain.KfToolsTestReport;
import com.stm.toolsTest.domain.KfToolsTestReportDetail;
import com.stm.toolsTest.service.KfToolsTestReportDetailService;
import com.stm.toolsTest.service.KfToolsTestReportService;

import net.sf.jasperreports.engine.JRException;

/**
 * 
 * @Description 试验报告controller
 * @CreateTime 2019年4月8日 下午5:06:09
 * @CreateBy 林龙
 */
@Controller
@RequestMapping("/toolsTest/testReview")
public class KfToolsTestReviewController extends BaseController
{
    private String prefix = "toolsTest/testReview";

    @Autowired
    private KfToolsTestReportService kfToolsTestReportService;
    @Autowired
    private KfToolsTestReportDetailService kfToolsTestReportDetailService;

    /**
     * 
     * @Description 跳转页面
     * @CreateTime 2019年4月4日 上午11:57:34
     * @CreateBy 林龙
     * @return
     */
    @RequiresPermissions("toolsTest:testReview:view")
    @GetMapping()
    public String testReview(ModelMap mmap)
    {
    	mmap.put("user", ShiroUtils.getSysUser());
        return prefix + "/testReview";
    }

    /**
     * 
     * @Description 分页查询
     * @CreateTime 2019年4月4日 上午11:57:25
     * @CreateBy 林龙
     * @param KfToolsTestReport
     * @return
     */
    @RequiresPermissions("toolsTest:testReview:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(KfToolsTestReport kfToolsTestReport)
    {
        startPage();
        List<KfToolsTestReport> list = kfToolsTestReportService.selectAllPageList(kfToolsTestReport);
        return getDataTable(list);
    }
    
    
    /**
     * 
     * @Description 新增弹出窗口
     * @CreateTime 2019年4月4日 上午11:57:11
     * @CreateBy 林龙
     * @return
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/testReview_add";
    }

    /**
     * 
     * @Description 新增保存 
     * @CreateTime 2019年4月4日 上午11:56:56
     * @CreateBy 林龙
     * @param KfToolsTestReport
     * @return
     */
    @RequiresPermissions("toolsTest:testReview:add")
    @Log(title = "试验报告管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestBody KfToolsTestReport kfToolsTestReport)
    {
    	ShiroUtils.clearCachedAuthorizationInfo();
    	kfToolsTestReport.setDprtcode(ShiroUtils.getSysUser().getDeptId());
    	kfToolsTestReport.setCreateBy(ShiroUtils.getLoginName()); //创建人
    	
        return toAjax(kfToolsTestReportService.insert(kfToolsTestReport));

    }
    
    
    
    /**
	 * 修改
	 */
    @RequiresPermissions("toolsTest:testReview:edit")
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") String id, ModelMap mmap)
	{
		mmap.put("kfToolsTestReport", kfToolsTestReportService.selectByPrimaryKey(id));
	    return prefix + "/testReview_edit";
	}
	
    
    
	/**
	 * 修改保存
	 * @throws Exception 
	 */
	@RequiresPermissions("toolsTest:testReview:edit")
	@Log(title = "试验报告", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(@RequestBody KfToolsTestReport kfToolsTestReport) throws Exception
	{
		kfToolsTestReport.setUpdateBy(ShiroUtils.getLoginName());
		return toAjax(kfToolsTestReportService.update(kfToolsTestReport));
	}
    
	/**
	 * 试验样品列表
	 */
	@PostMapping("/kfToolsTestReportDetailList")
	@ResponseBody
	public TableDataInfo kfToolsTestReportDetailList(KfToolsTestReportDetail kfToolsTestReportDetail)
	{
		startPage();
		List<KfToolsTestReportDetail> list = kfToolsTestReportDetailService.selectAllPageList(kfToolsTestReportDetail);
		return getDataTable(list);
	}
	
	 /**
     * 
     * @Description 删除
     * @CreateTime 2019年4月4日 上午11:56:27
     * @CreateBy 林龙
     * @param ids
     * @return
     */
    @RequiresPermissions("toolsTest:testReview:remove")
    @Log(title = "试验报告管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        try
        {
            return toAjax(kfToolsTestReportService.deleteByPrimary(ids));
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }
	
    /**
     * 
     * @Description 查看详情
     * @CreateTime 2019年4月8日 上午9:42:17
     * @CreateBy 林龙
     * @param id
     * @param mmap
     * @return
     */
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") String id, ModelMap mmap)
    {
    	mmap.put("kfToolsTestReport", kfToolsTestReportService.selectByPrimaryKey(id));
	    return prefix + "/testReview_detail";
    }
    
    
    /**
     * 
     * @Description 提交试验完成
     * @CreateTime 2019年4月8日 上午9:42:17
     * @CreateBy 林龙
     * @param id
     * @param mmap
     * @return
     * @throws IOException 
     * @throws JRException 
     */
    @PostMapping("/sendInspection")
    @ResponseBody
    public AjaxResult sendInspection(String ids)
    {
        try
        {
            return toAjax(kfToolsTestReportService.reviewByPrimary(ids));
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }
    
    /**
     * 多选选择工具
     */
    @GetMapping("/openSelectOrder")
    public String openSelectOrder( )
    {
    	
    	return "open/openSelectOrder";
    }
    
    

    
}